 //
//  Nuomishuo.m
//  解析
//
//  Created by lanou3g on 16/1/14.
//  Copyright © 2016年 WHB. All rights reserved.
//

#import "Nuomishuo.h"
#import "Shop.h"
#import "City.h"
#import "Tuandata.h"
#import "Cityqu.h"
#import "TuandanFenglei.h"
#import "ChaTuanId.h"
#import "ChaXtuan.h"
//糯米商户详情接口

//商户
//    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/shopinfo";

//    NSString *httpArg = @"shop_id=1745896";
//城市
//    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/cities";
//    NSString *httpArg = @"";

//  团单
//    NSString *httpUrl = @" http://apis.baidu.com/baidunuomi/openapi/dealdetail";
//    NSString *httpArg = @"deal_id=2327781";

//城市下的区
//   NSString *httpUrl = @" http://apis.baidu.com/baidunuomi/openapi/districts";
//    NSString *httpArg = @"city_id=来接城市中的city_id";


//团单(如 美食,)
//    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/categories";
//    NSString *httpArg = @"";

//团单id
//    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/dealshops";
//    NSString *httpArg = @"city_id=接上面的&deal_id=1228062";

//根据查询条件获取相应团单


//    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/searchdeals";
//    NSString *httpArg = @"city_id=100010000&cat_ids=326&subcat_ids=962%2C994&district_ids=394%2C395&bizarea_ids=1322%2C1328&location=116.4374%2C39.8719&keyword=%E9%87%91%E9%BC%8E%E8%BD%A9&radius=3000&sort=0&page=1&page_size=10&is_reservation_required=0";

//根据查询条件获取查询的商户信息

//    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/searchshops";
//    NSString *httpArg = @"city_id=100010000&cat_ids=326&subcat_ids=962%2C994&district_ids=394%2C395&bizarea_ids=1322%2C1328&location=116.418993%2C39.915597&keyword=%E4%BF%8F%E6%B1%9F%E5%8D%97&radius=3000&page=1&page_size=5&deals_per_shop=10";



@implementation Nuomishuo

-(NSMutableArray *)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg{
    
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"aaeecb8aa0e829df07ca2b87568aaec4" forHTTPHeaderField: @"apikey"];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
        } else {
            _unm = [(NSHTTPURLResponse *)response statusCode];
            _str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"HttpResponseCode:%ld", _unm);
//            NSLog(@"HttpResponseBody %@",responseString);
            
            _array = [NSMutableArray array];
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//            NSLog(@"%@",dic);
            _array = [NSMutableArray array];
            //城市
            if ([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/cities"]) {
                for (NSDictionary *dict in dic[@"cities"]) {
                    City *model = [[City alloc]init];
                    [model setValuesForKeysWithDictionary:dict];
                    [_array addObject:model];
                    NSLog(@"%@",model);
                }
                //商户
            }else if ([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/shopinfo"]){
                Shop *shopModel = [[Shop alloc]init];
                [shopModel setValuesForKeysWithDictionary:dic[@"shop"]];
                [_array addObject:shopModel];
                NSLog(@"%@",_array);
                //团单详情
            }else if([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/dealdetail"]){
                Tuandata *tumodel = [[Tuandata alloc]init];
                [tumodel setValuesForKeysWithDictionary:dic[@"deal"]];
                NSLog(@"===========%@",dic[@"deal"]);
                [_array addObject:tumodel];
                NSLog(@"%@",_array);
                //城市下的区
            }else if ([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/districts"]){
                
                for (NSDictionary *dict in dic[@"districts"]) {
                    NSLog(@"%@",dict);
                    Cityqu *qumodel = [[Cityqu alloc]init];
                    [qumodel setValuesForKeysWithDictionary:dict];
                    [_array addObject:qumodel];

                }
                for (Cityqu *model in _array) {
                    NSLog(@"%@",model);
                }
                
            }else if ([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/categories"]){
                for (NSDictionary *dict in dic[@"categories"]) {
                    TuandanFenglei *model = [[TuandanFenglei alloc]init];
                    [model setValuesForKeysWithDictionary:dict];
                    [_array addObject:model];
                }
                for (TuandanFenglei *model in _array) {
                    NSLog(@"%@",model);
                }
            }else if ([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/dealshops"]){
                for (NSDictionary *dict in dic[@"shops"]) {
                    ChaTuanId *model = [[ChaTuanId alloc]init];
                    [model setValuesForKeysWithDictionary:dict];
                    [_array addObject:model];
                }
                for (ChaTuanId *model in _array) {
                    NSLog(@"%@",model);
                }
            }else if ([httpUrl isEqualToString:@"http://apis.baidu.com/baidunuomi/openapi/searchdeals"]){
                NSLog(@"%@",dic);
                for (NSDictionary *dict in dic[@"deals"]) {
                    NSLog(@"%@",dict);
                    ChaXtuan *model = [[ChaXtuan alloc]init];
                    [model setValuesForKeysWithDictionary:dict];
                    [_array addObject:model];
                }
                for (ChaXtuan *model in _array) {
                    NSLog(@"%@",model);
                }
            }
        }
        
    }];
  
    
    [task resume];
 
    return _array;
}



@end
